﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using ShakeIt_.ServiceReference2;
using System.ComponentModel;

namespace ShakeIt_
{
    public partial class commentListPage : PhoneApplicationPage
    {
        private List<_UserComment> _comments;

        public List<_UserComment> Comments
        {
            set
            {
                if (value != _comments)
                {
                    _comments = value;
                    NotifyPropertyChanged("Comments");
                }
            }
            get
            {
                return _comments;
            }
        }

        public commentListPage()
        {
            InitializeComponent();
            /* 评价从好到差： Data.commentResearch.Sort(ScoreGoodFirstCompration);
             * 评价从差到好： Data.commentResearch.Sort(ScoreBadFirstCompration);
             * 按时间排序： Data.commentResearch.Sort(CommentTimeCompration);
             * 输出就用 Data.commentResearch 显示
             */
        }

        private static int ScoreBadFirstCompration(_UserComment obj1, _UserComment obj2)
        {
            int res = 0;
            if ((obj1 == null) && (obj2 == null))
            {
                return 0;
            }
            else if ((obj1 != null) && (obj2 == null))
            {
                return 1;
            }
            else if ((obj1 == null) && (obj2 != null))
            {
                return -1;
            }
            if (obj1.score > obj2.score)
            {
                res = 1;
            }
            else if (obj1.score < obj2.score)
            {
                res = -1;
            }
            return res;
        }

        private static int ScoreGoodFirstCompration(_UserComment obj1, _UserComment obj2)
        {
            int res = 0;
            if ((obj1 == null) && (obj2 == null))
            {
                return 0;
            }
            else if ((obj1 != null) && (obj2 == null))
            {
                return 1;
            }
            else if ((obj1 == null) && (obj2 != null))
            {
                return -1;
            }
            if (obj1.score > obj2.score)
            {
                res = -1;
            }
            else if (obj1.score < obj2.score)
            {
                res = 1;
            }
            return res;
        }

        private static int CommentTimeCompration(_UserComment obj1, _UserComment obj2)
        {
            int res = 0;
            if ((obj1 == null) && (obj2 == null))
            {
                return 0;
            }
            else if ((obj1 != null) && (obj2 == null))
            {
                return 1;
            }
            else if ((obj1 == null) && (obj2 != null))
            {
                return -1;
            }
            if (obj1.time > obj2.time)
            {
                res = -1;
            }
            else if (obj1.time < obj2.time)
            {
                res = 1;
            }
            return res;
        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            PageTitle.Text = Data.userFocusedName + "的评价";
            textBlock1.Text = "累计评价：" + Data.commentResearch.Count + "次";

            Comments = null;
            commentBox.ItemsSource = null;

            Comments = Data.commentResearch;
            commentBox.ItemsSource = Comments;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Comments = null;
            commentBox.ItemsSource = null;
            Data.commentResearch.Sort(CommentTimeCompration);
            Comments = Data.commentResearch;
            commentBox.ItemsSource = Comments;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Comments = null;
            commentBox.ItemsSource = null;
            Data.commentResearch.Sort(ScoreGoodFirstCompration);
            Comments = Data.commentResearch;
            commentBox.ItemsSource = Comments;
        }
    }

    public partial class commentListPage : INotifyPropertyChanged
    {
        #region Property Changed

        private void NotifyPropertyChanged(string propertyName)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        #endregion
    }
}